# !usr/bin/env python
# -*- coding:utf-8 -*-

'''
 Description  : 
 Version      : 1.0
 Author        : qww
 Date         : 2021-05-02 16:57:13
 LastEditors  : qww
 LastEditTime : 2021-05-02 19:32:00
'''

import json
from flask import request
from flask import Blueprint
from settings import DBConfig
from utils.db_util import DBUtil
from result_data.result_data import result_data, Movie, FailCode, SuccessCode


movies_bp = Blueprint('movies', __name__)


def __pre_result_data(code, msg, data, count):
    result_data['code'] = code
    result_data['msg'] = msg
    result_data['count'] = count
    result_data['data'] = data

    return result_data


def __pre__data(code, msg, rs):
    """
    封装数据
    :param rs: 结果集
    :return: 传给前端的数据
    """
    items = []
    count = len(rs)
    for item in rs:
        m = Movie()
        m.id, m.name = item[0], item[1]
        m.img_url, m.play_url = item[2], item[3]
        m.describe, m.video_url = item[4], item[5]
        m.score, m.actor = item[6], item[7]
        m.scenario = item[8]
        items.append(m.__dict__)
    __pre_result_data(code, msg, items, count)

    return result_data


@movies_bp.route("/movies/one")
def queryOneById():
    dbUtil = DBUtil(DBConfig.host, DBConfig.user, DBConfig.password, DBConfig.database, DBConfig.port)
    param = request.args
    if len(param) == 0:
        result_data = __pre_result_data(FailCode.PARAM_ERROR, "fail, 请求参数错误", [], 0)
        return json.dumps(result_data, indent=2, ensure_ascii=False)
    id = param.get('id')
    sql = 'select `id`, `name`, img_url, play_url, `describe`, `video_url`, `score`, `actor`, `scenario` from `tbl_movies` where id=%s'
    rs = dbUtil.queryAll(sql, args=[id])
    if len(rs) == 0:
        result_data = __pre_result_data(FailCode.NOT_EXIST_ERROR, "fail, 请求数据不存在", [], 0)
        return json.dumps(result_data, indent=2, ensure_ascii=False)
    result_data = __pre__data(SuccessCode.SUCCESS, "success, 请求成功", rs)
    return json.dumps(result_data, indent=2, ensure_ascii=False)


@movies_bp.route("/movies")
def queryCount():
    dbUtil = DBUtil(DBConfig.host, DBConfig.user, DBConfig.password, DBConfig.database, DBConfig.port)
    param = request.args
    if len(param) == 0:
        result_data = __pre_result_data(FailCode.PARAM_ERROR, "fail, 请求参数错误", [], 0)
        return json.dumps(result_data, indent=2, ensure_ascii=False)
    startIndex = param.get('startIndex', default=0, type=int)
    sql = 'select `id`, `name`, img_url, play_url, `describe`, `video_url`, `score`, `actor`, `scenario` from `tbl_movies` limit %s,6'
    rs = dbUtil.queryAll(sql, args=[startIndex])
    result_data = __pre__data(SuccessCode.SUCCESS, "success, 请求成功", rs)
    return json.dumps(result_data, indent=2, ensure_ascii=False)


@movies_bp.route("/movies/all")
def queryAll():
    dbUtil = DBUtil(DBConfig.host, DBConfig.user, DBConfig.password, DBConfig.database, DBConfig.port)
    sql = 'select `id`, `name`, img_url, play_url, `describe`, `video_url`, `score`, `actor`, `scenario` from `tbl_movies`'
    rs = dbUtil.queryAll(sql)
    result_data = __pre__data(SuccessCode.SUCCESS, "success, 请求成功", rs)
    return json.dumps(result_data, indent=2, ensure_ascii=False)

